﻿Imports DTO
Imports System.Data.SqlClient


Public Class PhieuMuon_DAO

    Public Shared Function loadPhieuMuon() As List(Of PhieuMuon_DTO)
        Dim dt As DataTable = DataProvider.loadDataTable("select *from phieumuon")

        Dim ds As New List(Of PhieuMuon_DTO)
        For Each r As DataRow In dt.Rows
            Dim pm As New PhieuMuon_DTO
            pm.maMuon = r(0)
            pm.ngayMuon = r(1).ToString
            pm.ngayHetHan = r(2).ToString
            pm.maTheDG = r(3)
            ds.Add(pm)
        Next

        Return ds
    End Function

    Public Shared Sub themPhieuMuon(ByVal pm As PhieuMuon_DTO, ByVal ds As List(Of ChiTietPhieuMuon_DTO))
        Dim con As SqlConnection = DataProvider.ConnectToDB
        Try
            Dim com As New SqlCommand
            com.CommandText = "PhieuMuon_Them"
            com.CommandType = CommandType.StoredProcedure
            com.Connection = con

            Dim para As New SqlParameter("ngaymuon", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = pm.ngayMuon
            com.Parameters.Add(para)

            para = New SqlParameter("ngayhethan", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = pm.ngayHetHan
            com.Parameters.Add(para)

            para = New SqlParameter("mathedg", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = pm.maTheDG
            com.Parameters.Add(para)

            para = New SqlParameter("mamuon", SqlDbType.Int)
            para.Direction = ParameterDirection.Output
            com.Parameters.Add(para)

            com.ExecuteNonQuery()

            Dim mamuon As Integer = com.Parameters("mamuon").Value

            For Each ct As ChiTietPhieuMuon_DTO In ds
                Dim sql As String = String.Format("insert into chitietphieumuon values({0},{1},{2},{3},1)", _
                                        mamuon, ct.maDauSach, ct.sLSach, ct.tienTheChan)

                DataProvider.ExecuteNonQuery(sql)
            Next


        Catch ex As Exception
            Throw New Exception("Vui lòng kiểm tra thông tin")
        End Try

        con.Close()
    End Sub
End Class
